﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		ControlUtil 
 * 
 * Created:		2013-7-23 15:42:21
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System.Data;
using System.Web.UI.WebControls;

namespace Besture.Utils
{
    public class ControlUtil
    {
        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <param name="sourceTable"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        public static void BindDropDownList(DropDownList dropDownList, DataTable sourceTable, string dataTextField, string dataValueField)
        {
            const string defaultItemStr = "---请选择---";
            if (sourceTable != null)
            {
                dropDownList.DataSource = sourceTable;
                dropDownList.DataTextField = dataTextField;
                dropDownList.DataValueField = dataValueField;
                dropDownList.DataBind();
            }
            dropDownList.Items.Insert(0, new ListItem(defaultItemStr, ""));
            dropDownList.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <param name="sourceTable"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        public static void BindDropDownListClass(DropDownList dropDownList, DataTable sourceTable, string dataTextField, string dataValueField)
        {
            const string defaultItemStr = "---请选择---";
            dropDownList.DataSource = sourceTable;
            dropDownList.DataTextField = dataTextField;
            dropDownList.DataValueField = dataValueField;
            dropDownList.DataBind();
            dropDownList.Items.Insert(0, new ListItem(defaultItemStr, ""));
            dropDownList.Items.Insert(1, new ListItem("顶级类别", "0"));
            dropDownList.SelectedIndex = 0;
        }


        /// <summary>
        /// 初始化下拉框
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="initText"></param>
        public static void InitDropDownList(DropDownList dropDownList, string initText)
        {
            dropDownList.Items.Clear();
            string defaultItemStr = string.IsNullOrEmpty(initText) ? "---请选择---" : initText;
            dropDownList.Items.Insert(0, new ListItem(defaultItemStr, ""));
            dropDownList.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <param name="sourceTable"></param>
        /// <param name="dataTextField"></param>
        /// <param name="dataValueField"></param>
        public static void BindDropDown(DropDownList dropDownList, DataTable sourceTable, string dataTextField, string dataValueField)
        {
            if (sourceTable != null)
            {
                dropDownList.DataSource = sourceTable;
                dropDownList.DataTextField = dataTextField;
                dropDownList.DataValueField = dataValueField;
                dropDownList.DataBind();
                dropDownList.Items.Insert(0, new ListItem("请选择", ""));
                dropDownList.SelectedIndex = 0;
            }
        }
    }
}
